package com.itheima.reggie.controller;

import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired(required = false)
    private DishService dishService;

    @Autowired
    private HttpSession session;

    /**
     * 添加菜功能
     * @param dishDto DTO类,Data Transfer Object(数据传输对象)，一般用于展示层与服务层之间的数据传输。
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        Long empID = (Long) session.getAttribute("Employee");
        //补全信息
        dishDto.setCreateUser(empID);
        dishDto.setUpdateUser(empID);
        dishService.save(dishDto);
        return R.success("添加菜成功");
    }

    /**
     * 分页查询功能
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize, String name) {
        R<Page<DishDto>> result = dishService.page(page, pageSize, name);
        return result;
    }

    /**
     * 根据ID查找DishDto
     * @param dishId
     * @return
     */
    @GetMapping("/{dishId}")
    public R<DishDto> findById(@PathVariable("dishId") Long dishId) {
        R<DishDto> result = dishService.findById(dishId);
        return result;

    }

    /**
     * 修改菜功能
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        Long empID = (Long) session.getAttribute("Employee");
        dishDto.setUpdateUser(empID);
        dishService.update(dishDto);
        return R.success("修改菜成功");
    }


    /**
     * 移动端显示菜品
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> findByCategoryId(Long categoryId,Integer status){
        List<DishDto> dishDtoList = dishService.findByCategoryId(categoryId,status);
        return R.success(dishDtoList);
    }


    /**
     * 菜品停售启售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable Integer status,
                                  @RequestParam List<Long> ids){
        dishService.updateStatus(status,ids);
        return R.success("修改成功");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }
}
